﻿using System.Globalization;
using System.Text.RegularExpressions;
using System.Windows;

namespace Oxage.CNC.Models
{
	public class LineInline : XamlPathDataCodeInline
	{
		#region Constructors
		public LineInline()
		{
		}

		public LineInline(double x, double y)
		{
			this.EndPoint = new Point(x, y);
		}

		public LineInline(string inline)
		{
			Parse(inline);
		}
		#endregion

		#region Public properties
		public Point EndPoint
		{
			get;
			set;
		}
		#endregion

		#region Public methods
		public override void Parse(string inline)
		{
			var match = Regex.Match(inline, @"(?<code>\w)\s*(?<x>[\d\-\+\.]+)\s*,\s*(?<y>[\d\-\+\.]+)", RegexOptions.IgnoreCase);
			if (match.Success)
			{
				this.Code = match.Groups["code"].Value[0];
				double x = double.Parse(match.Groups["x"].Value, CultureInfo.InvariantCulture);
				double y = double.Parse(match.Groups["y"].Value, CultureInfo.InvariantCulture);
				this.EndPoint = new Point(x, y);
			}
		}

		public override string ToString()
		{
			string x = this.EndPoint.X.ToString(CultureInfo.InvariantCulture);
			string y = this.EndPoint.Y.ToString(CultureInfo.InvariantCulture);
			return string.Format("L{0},{1}", x, y);
		}
		#endregion
	}
}
